#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

int n,m;
char S[1005],T[1005];
int dp[1005][1005];

int main()
{
	scanf("%d%d",&n,&m);
	scanf("%s",S+1);
	scanf("%s",T+1);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++) 
		if (S[i]==T[j]) dp[i][j]=dp[i-1][j-1]+1;
		else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
	printf("%d",dp[n][m]);
    return 0;
}
